package com.sdp.right.control;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.QueryColumn;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.cache.clean.RightCleanCache;
import com.sdp.right.db.sys_menu.service.ISysMenuService;
import com.sdp.right.db.sys_role_menu.service.ISysRoleMenuService;
import com.sdp.right.entity.SysMenu;

@RestController
@RequestMapping("/SysMenuController")
public class SysMenuController {
	
	@Autowired
	private ISysMenuService iSysMenuService;
	
	@Autowired
	private ISysRoleMenuService iSysRoleMenuService;
	
	@Autowired
	private RightCleanCache rightCleanCache;

	
	@PostMapping(value = "/queryMenuTableTree")
	public Result<?> queryMenuTableTree(@RequestBody DataCenter dataCenter) {
		String systemNo = "";
		List<QueryColumn> queryFrom = dataCenter.getQueryForm();
		if(queryFrom!=null && queryFrom.size()>0) {
			for(QueryColumn qc : queryFrom) {
				if(CoreConstantsEnum.header中的模块参数名.getCode().equals(qc.getField())) {
					systemNo = qc.getVal();
					break;
				}
			}
		}
		if(!StringUtils.isEmpty(systemNo)) {
			return Result.ok(iSysMenuService.queryMenuTableTree(systemNo));
		}else {
			return Result.error("系统编号不存在，查询失败");
		}
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysMenu sysMenu) {
		if(sysMenu==null) {
			return Result.error("菜单信息为空");
		}
		if(!StringUtils.isEmpty(sysMenu.getMenuPath())) {
			sysMenu.setMenuPath(sysMenu.getMenuPath().trim());
		}
		if(!StringUtils.isEmpty(sysMenu.getMenuComponent())) {
			sysMenu.setMenuComponent(sysMenu.getMenuComponent().trim());
		}
		if(!StringUtils.isEmpty(sysMenu.getParentId())) {
			sysMenu.setParentLevel(iSysMenuService.queryParentMenuLevel(sysMenu.getParentId())+"["+sysMenu.getParentId()+"]");
		}
		
		if(StringUtils.isEmpty(sysMenu.getKeyId())) {
			//如果新添加的菜单有父节点，则从角色菜单表中，将此父节点菜单删除，为了保证角色授权菜单权限时，回显正常
			//主要是TREE组件的回显
			if(!StringUtils.isEmpty(sysMenu.getParentId())) {
				iSysRoleMenuService.removeSysRoleMenuByMenu(sysMenu.getParentId());
			}
			iSysMenuService.save(sysMenu);
		}else {
			iSysMenuService.updateById(sysMenu);
		}
		rightCleanCache.cleanMenuCache();
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("传入参数不完整，不能删除");
		}
		int count = iSysMenuService.queryChildMenuCount(keyId);
		if(count>0) {
			return Result.error("还有子菜单，不能删除");
		}
		iSysMenuService.removeById(keyId);
		
		//删除角色权限表中此菜单信息
		iSysRoleMenuService.removeSysRoleMenuByMenu(keyId);
		rightCleanCache.cleanMenuCache();
		return Result.ok();
	}
	
}
